from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d, has_prize=True):
        super().__init__(d, has_prize)
        self.lv = 0

    def get_name(self):
        return "宫爆老奶奶"

    def game_alert(self):
        self.alert_close_by_file('./game/img/宫爆老奶奶')

    def swipe_down(self, duration=0.5):
        """
        向下滑半屏
        :return:
        """
        self.log_msg("向下滑半屏")
        w, h = self.d.window_size()
        self.d.swipe(w / 2, h / 2, w / 2, h - 100, duration)

    def play_alert(self):
        if not self.img_exit("./game/img/宫爆老奶奶/play_alert.png", 1):
            self.log_msg("角色未升级")
            return False

        while self.img_exit("./game/img/宫爆老奶奶/play_alert.png", 2):
            self.log_msg("角色升级选择技能")
            self.click_top(0.5)
            self.lv += 1

        return True

    def play_alert_box(self):
        if not self.img_exit("./game/img/宫爆老奶奶/play_alert_box.png", 1):
            self.log_msg("不存在神秘宝箱")
            return False
        self.log_msg("存在神秘宝箱，点击开启")
        self.img_click("./game/img/宫爆老奶奶/play_alert_box_01.png")
        while not self.img_exit("./game/img/宫爆老奶奶/play_alert_box_done.png", 2):
            self.log_msg("等待神秘宝箱开启...")
            self.d.sleep(2)
        self.log_msg("神秘宝箱，开启完成，点击确定")
        self.img_click("./game/img/宫爆老奶奶/play_alert_box_done_1.png")

        return True

    def game_play(self):
        self.lv = 0
        while True:
            self.play_alert()
            self.play_alert_box()

            if not self.img_exit('./game/img/宫爆老奶奶/play_location.png', 2):
                self.log_msg("角色未到左下角，进行移动")
                self.swipe_left(1)
                self.swipe_down(1)
                continue
            self.log_msg("角色已到左下角")

            if self.lv > 10:
                if self.check_done():
                    break
            self.log_msg("战斗中")
            self.d.sleep(5)

        self.log_msg("任务已完成")

    def handle_main(self):
        self.log_msg("等待进入游戏主界面")
        if not self.img_exit('./game/img/宫爆老奶奶/game_main.png', 50):
            return False
        self.log_msg("已进入游戏主界面")

        self.game_alert()

        if not self.img_exit('./game/img/宫爆老奶奶/play_01.png', 3):
            self.log_msg("游戏主界面不是关卡1")
            self.log_msg("点击选择关卡")
            self.click_top(0.5)
            while not self.img_exit('./game/img/宫爆老奶奶/play_02.png', 2):
                self.swipe_right(0.2)

            self.log_msg("找到关卡1，点击选择")
            self.img_click('./game/img/宫爆老奶奶/play_select.png')
            self.d.sleep(1)
        self.log_msg("点击开始游戏")
        self.img_click('./game/img/宫爆老奶奶/play_start.png')

        self.log_msg("等待进入战斗界面")
        self.img_exit('./game/img/宫爆老奶奶/play_main.png', 20)
        self.log_msg("已进入战斗界面")

        self.game_play()

        return True
